/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.*;
import com.sun.org.apache.xml.internal.security.utils.HelperNodeList;
import java.sql.Savepoint;
import java.sql.Types;
import java.util.ArrayList;
import pojo.CTPhieuNhapPOJO;

/**
 *
 * @author HP
 */
public class CTPhieuNhapDAO {

    MySqlDataAccessHelper helper = new MySqlDataAccessHelper();

    /**
     * Ham Them Phieu Nhap Tra ve MaPhieuNhap Vua Them
     * @param pnh
     * @return
     */
    public int ThemCTPhieuNhap(CTPhieuNhapPOJO ct) {

        int mactpn = 0;
        try {

            Connection cn = null;
            //Buoc 2: Mo kert noi
            helper.open();
            cn = (Connection) helper.getConnection();
            cn.setAutoCommit(false);
            // Statement state=(Statement) cn.createStatement();
            Savepoint save = cn.setSavepoint();
            //Buoc 3: Thuc thi cau truy van
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_Them_CTPhieuNhap(?,?,?,?,?)}");
            statement.setInt(1, ct.getMaPhieuNhap());
            statement.setString(2, ct.getMaSach());
            statement.setInt(3,ct.getSoLuongNhap());
            statement.setFloat(4, ct.getDonGiaNhap());
            statement.registerOutParameter(5, Types.INTEGER);
            //int n=-1;
            boolean kq = statement.execute();
            mactpn = statement.getInt(2);
            if (kq == true) {
                cn.commit();

            } else {
                cn.rollback(save);
            }
        } catch (Exception ex) {
        }
        helper.close();
        return mactpn;
    }

     public int ThemChiTietPhieuNhap_sql(CTPhieuNhapPOJO ct)
{
    boolean kq=false;
    int mapn=0;
    try{

        //Buoc 1: Khoi tao va tao ket noi
        Connection cn=null;
        //Buoc 2: Mo kert noi
        helper.open();
        cn=helper.getConnection();
        cn.setAutoCommit(false);
        Savepoint save=cn.setSavepoint();
        //Buoc 3: Thuc thi cau truy van
        String sql1="INSERT INTO chitietphieunhap(MaPhieuNhap,MaSach,SoLuongNhap,DonGiaNhap)VALUES("+ct.getMaPhieuNhap()+",'"+ct.getMaSach()+"',"+ct.getSoLuongNhap()+","+ct.getDonGiaNhap()+")";

        Statement stm= (Statement) cn.createStatement();
        //pre.setDate(1, today);
      //  pre.setFloat(2,hd.getTongTien());
        int n=-1;
        n=stm.executeUpdate(sql1,Statement.RETURN_GENERATED_KEYS);

        if(n>0)
        {
            mapn=20;
            //ResultSet rs=stm.getGeneratedKeys();
           // mapn=rs.getInt(1);
            cn.commit();
            kq=true;
        }
        else
        {
            cn.rollback(save);
        }
    }
    catch(Exception ex)
    {

    }
    helper.close();
    return mapn;
}
}
